<?php

namespace app\shop\controller\product;

use app\shop\controller\Controller;
use app\shop\model\product\Attribute as AttributeModel;

/**
 * 商品属性控制器
 */
class Attribute extends Controller
{
    /**
     * 列表
     */
    public function index()
    {
        $model = new AttributeModel;
        $list = $model->getList($this->postData());
        return $this->renderSuccess('', compact('list'));
    }

    /**
     * 添加
     */
    public function add()
    {
        $model = new AttributeModel();
        if ($model->add($this->postData())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑
     */
    public function edit($attribute_id)
    {
        // 模板详情
        $model = AttributeModel::detail($attribute_id);
        // 更新记录
        if ($model->edit($this->postData())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除商品
     */
    public function delete($attribute_id)
    {
        $model = new AttributeModel;
        if (!$model->setDelete($attribute_id)) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
}
